package com.tencent.wework.list

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView


/**
 * Created by huangwei on 2019/5/26.
 */
open abstract class BaseRecyclerCell<DATA>(public var data:DATA):OnViewRecycledListener{
    var viewHolder:BaseViewHolder?=null
    var adapter: BaseRecyclerAdapter<out BaseRecyclerCell<out Any>>? = null
    var recyclerView: RecyclerView?=null

    abstract fun createViewHolder(parent: ViewGroup): BaseViewHolder

    protected abstract fun onBindViewHolder(viewHolder: BaseViewHolder)

    protected open fun onBindViewHolder(viewHolder: BaseViewHolder, payloads: List<Any>) {}

    open fun getType(): Int{
        return this::class.java.hashCode()
    }

    override fun onViewRecycled() {
        viewHolder = null
    }

    fun bindViewHolder(viewHolder: BaseViewHolder) {
        bindViewHolder(viewHolder, null)
    }

    fun bindViewHolder(viewHolder: BaseViewHolder, payloads: List<Any>?) {
        this.viewHolder = viewHolder
        viewHolder.onViewRecycledListener = this

        if (payloads == null || payloads.isEmpty()) {
            onBindViewHolder(viewHolder)
        } else {
            onBindViewHolder(viewHolder, payloads)
        }
    }

    fun updateData(data: DATA) {
        this.data = data
        viewHolder?.apply { onBindViewHolder(this) }
    }

    internal fun onDestroyView() {
        viewHolder = null
        adapter = null
        recyclerView = null
    }
    /**判断两个Cell是否相等，返回true则视为相同的Cell*/
    internal fun areItemsTheSame(cell: BaseRecyclerCell<*>?): Boolean {
        if (cell == null) {
            return false
        }
        return if (javaClass.isInstance(cell)) {
            if (data is IDiffId) {
                (data as IDiffId).getDiffId() == (cell.data  as IDiffId).getDiffId()
            } else {
                data != null && data == cell.data
            }
        } else false
    }

    /**走到这里表示两个cell的data是同一个，这里只是判断一下里面内容有没有局部更新，如果返回true，会继续调用下面
    的[getChangePayload]函数，获取局部更新的参数*/
    internal fun areContentsTheSame(cell: BaseRecyclerCell<*>?): Boolean {
        if (cell == null) {
            return false
        }
        val diffMode = getDiffMode()
        return if (diffMode != null && diffMode === DiffMode.UNIQUE) {
            javaClass.isInstance(cell)
        } else isDataEquals(cell.data)
        /** 默认是[DiffMode.DATA]模式 */
    }

    /**
     * 返回局部更新的参数，例如只需更新联系人的头像，可以返回头像的Url，这里就会调用带payloads参数的[onBindViewHolder]
     * 函数，在里面只做更新头像操作即可
     */
    internal fun getChangePayload(cell: BaseRecyclerCell<*>): Any? {
        return null
    }

    protected fun isDataEquals(b: Any?): Boolean {
        return data == b
    }

    protected fun getDiffMode(): DiffMode? {
        return DiffMode.DATA
    }

    protected fun inflateLayout(@LayoutRes layoutRes:Int, parent: ViewGroup): BaseViewHolder {
        return BaseViewHolder(LayoutInflater.from(parent.context).inflate(layoutRes,parent,false))
    }
}